// pages/mystore/storename/index.js
import { apiUrls, makeRequest } from '../../../utils/api';
const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    is_follow_time: true,
    is_active: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    that.setData({
      id: options.id
    })
    that.wxValidate = App.wxValidate(
      {
        name: {
          required: true,
          minlength: 2,
          maxlength: 10,
        }
      },
      {
        name: {
          required: '请填写商铺名称',
          minlength: '商铺名称至少两个字哦'
        }
      }
    );
    makeRequest({
      url: apiUrls.getShopDetail(options.id),
      method: 'GET',
      data: {
        include: 'category,manager,area'
      },
      success: function (res) {
        that.setData({
          is_follow_time: res.data.data.is_follow_time,
          is_active: res.data.data.is_active
        })
      }
    })
  },
  follow(e) {
    let that = this;

    makeRequest({
      url: apiUrls.putFollowEdit(that.data.id),
      method: 'PUT',
      data: {
        is_follow_time: e.detail.value
      },
      success: function (res) {
        if (res.statusCode === 200) {
          that.setData({
            is_follow_time: e.detail.value
          });
        } else {
          wx.showToast({
            title:'系统错误',
            image: '/images/error.png',
            duration: 500
          });
        }
      }
    });
  },
  active(e) {
    let that = this;
    makeRequest({
      url: apiUrls.putStatusEdit(that.data.id),
      method: 'PUT',
      data: {
        is_active: e.detail.value,
        reason:'打烊啦！'
      },
      success: function (res) {
        if (res.statusCode === 200) {
          that.setData({
            is_active: e.detail.value
          });
        } else {
          wx.showToast({
            title: '系统错误',
            image: '/images/error.png',
            duration: 500
          });
        }
      }
    });
  }
})